
#include "gh.cu.h"
#include "gh.h"

int ghCUModuleLoad(CUmodule *module, const char *ptxSource) {
    CUjit_option opt[5];
    void *optVal[5];
    char infoLog[1024];
    char errorLog[1024];

    opt[0] = CU_JIT_INFO_LOG_BUFFER;
    optVal[0] = infoLog;
    opt[1] = CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES;
    optVal[1] = (void*) sizeof(infoLog);
    opt[2] = CU_JIT_ERROR_LOG_BUFFER;
    optVal[2] = errorLog;
    opt[3] = CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES;
    optVal[3] = (void*) sizeof(errorLog);

    if (cuModuleLoadDataEx(module, ptxSource, 4, opt, optVal) != CUDA_SUCCESS) {
        ghErrorSetInfo(__FILE__, __LINE__, "PTX program source is invalid. Error Log:\n%s", errorLog);
        return -1;
    }

#if GH_APP_TRACE == 1
    if (infoLog[0] != 0) {
        ghTrace(__FILE__, __LINE__, "Info Log:\n%s\n", infoLog);
    }
#endif

    return 0;
}
